package com.tuanzi.loan;

import java.nio.charset.StandardCharsets;

import javax.sql.DataSource;

import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.activiti.engine.impl.persistence.StrongUuidGenerator;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import com.google.common.collect.Lists;
import com.tuanzi.loan.workflow.factory.CustomGroupEntityManagerFactory;
import com.tuanzi.loan.workflow.factory.CustomUserEntityManagerFactory;
import com.tuanzi.loan.workflow.listener.BussinessEventListener;
import com.tuanzi.loan.workflow.listener.ScriptEventListener;

/**
 * @author Ray 2017/10/01
 */
@Configuration
public class ActivitiInitializer {

	@Autowired
	private DataSource dataSource;
	@Autowired
	private PlatformTransactionManager transactionManager;
	@Autowired
	private BussinessEventListener bussinessEventListener;
	@Autowired
	private ScriptEventListener scriptEventListener;
	@Autowired
	private CustomUserEntityManagerFactory customUserEntityManagerFactory;
	@Autowired
	private CustomGroupEntityManagerFactory customGroupEntityManagerFactory;
	@Autowired
	private ApplicationContext applicationContext;

	@Bean
	public SpringProcessEngineConfiguration processEngineConfiguration() {
		SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
		configuration.setApplicationContext(applicationContext);
		configuration.setDatabaseType("mysql");
		configuration.setDatabaseSchemaUpdate("false");
		configuration.setDataSource(dataSource);
		configuration.setJobExecutorActivate(false);
		configuration.setActivityFontName("宋体");
		configuration.setLabelFontName("宋体");
		configuration.setDatabaseSchemaUpdate("none");
		configuration.setXmlEncoding(StandardCharsets.UTF_8.name());
		configuration.setEnableDatabaseEventLogging(false);
		configuration.setIdGenerator(idGenerator());
		configuration.setTransactionManager(transactionManager);
		configuration.setEventListeners(Lists.newArrayList(bussinessEventListener, scriptEventListener));
		configuration.setCustomSessionFactories(Lists.newArrayList(customUserEntityManagerFactory, customGroupEntityManagerFactory));
		return configuration;
	}

	@Bean
	public IdGenerator idGenerator() {
		return new StrongUuidGenerator();
	}

	@Bean
	public RepositoryService repositoryService(ProcessEngine processEngine) {
		return processEngine.getRepositoryService();
	}

	@Bean
	public RuntimeService runtimeService(ProcessEngine processEngine) {
		return processEngine.getRuntimeService();
	}

	@Bean
	public TaskService taskService(ProcessEngine processEngine) {
		return processEngine.getTaskService();
	}

	@Bean
	public FormService formService(ProcessEngine processEngine) {
		return processEngine.getFormService();
	}

	@Bean
	public HistoryService historyService(ProcessEngine processEngine) {
		return processEngine.getHistoryService();
	}

	@Bean
	public ManagementService managementService(ProcessEngine processEngine) {
		return processEngine.getManagementService();
	}

	@Bean
	public IdentityService identityService(ProcessEngine processEngine) {
		return processEngine.getIdentityService();
	}

}
